<?php
/**
 * //w/w/w.s/h/u/o/t//u/p/u c/o/m  说/图/谱/源/码/分/享
 */
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/7/6 0006
 * Time: 09:30
 */
defined("IN_IA") or exit("Access Denied");
checklogin();  //验证是否登录
global $_W,$_GPC;
$uniacid=$_W['uniacid'];
$op=$_GPC['op'] ? $_GPC['op'] : "card_index";
if($op=='card_index'){
    $condition=array();
    $condition['uniacid']=$uniacid;
    if(!empty($_GPC['status'])){
        $status=$_GPC['status'];
        if($status==3){
            $condition['status']=0;
        }else{
            $condition['status']=$status;
        }
    }
    if(!empty($_GPC['card_num'])){
        $card_num=$_GPC['card_num'];
        $condition['card_num LIKE']='%'.$_GPC['card_num'].'%';
    }

    if(!empty($_GPC['uid'])){
        $uid=$_GPC['uid'];
        $condition['uid']=$uid;
    }
    if(empty($_GPC['status']) && empty($_GPC['card_num'])&&empty($_GPC['uid']) &&empty($_GPC['nickname'])){
        $listCount = pdo_getall("cqkundian_farm_card", $condition);
        $total = count($listCount);   //数据的总条数
        $pageSize = 10; //每页显示的数据条数
        $pageIndex = intval($_GPC['page']) ? intval($_GPC['page']) : 1;  //当前页
        $pager = pagination($total, $pageIndex, $pageSize);
        $list = pdo_getall("cqkundian_farm_card", $condition, '', '', 'import_time desc', array($pageIndex, $pageSize));
    }else{
        if(!empty($_GPC['nickname'])){
            $nickname=$_GPC['nickname'];
            $user=pdo_getall('cqkundian_farm_user',array('uniacid'=>$uniacid,'nickname LIKE'=>'%'.$nickname.'%'));
            $uids=array();
            for ($i=0;$i<count($user);$i++){
                $uids[]=$user[$i]['uid'];
            }
            $condition['uid in']=$uids;
        }

        $list=pdo_getall("cqkundian_farm_card",$condition,'','','import_time desc');
    }
    for ($i=0;$i<count($list);$i++){
        if($list[$i]['uid']!=0){
            $user=pdo_get('cqkundian_farm_user',array('uniacid'=>$uniacid,'uid'=>$list[$i]['uid']));
            $list[$i]['nickname']=$user['nickname'] ."（UID：".$list[$i]['uid']."）";
        }else{
            $list[$i]['nickname']='--';
        }
        $list[$i]['import_time']=date("Y-m-d H:i:s",$list[$i]['import_time']);
        if($list[$i]['use_time']){
            $list[$i]['use_time']=date("Y-m-d H:i:s",$list[$i]['use_time']);
        }else{
            $list[$i]['use_time']='--:--';
        }
        if($list[$i]['expire_time']){
            $list[$i]['expire_time']=date("Y-m-d H:i:s",$list[$i]['expire_time']);
        }else{
            $list[$i]['expire_time']='--:--';
        }
    }
    include $this->template("web/card/index");
}

//文件导入
if($op=='uploadExcel'){
    require_once("Org/PHPExcel.class.php");
    if(!empty($_FILES)) {
        if (@is_uploaded_file($_FILES['upfile']['tmp_name'])) {
            $upfile = $_FILES["upfile"];
            //获取数组里面的值
            $name = $upfile["name"];//上传文件的文件名
            $type = $upfile["type"];//上传文件的类型
            $size = $upfile["size"];//上传文件的大小
            $tmp_name = $upfile["tmp_name"];//上传文件的临时存放路径
            move_uploaded_file($tmp_name, dirname(__FILE__) . $name);//将上传到服务器临时文件夹的文件重新移动到新位置
            $file_name = dirname(__FILE__) . $name;
            $error = $upfile["error"];//上传后系统返回的值
            if ($error == 0) {
                //文件名为文件路径和文件名的拼接字符串
                $objReader = \PHPExcel_IOFactory::createReader('Excel5');//创建读取实例
                /*
                 * log()//方法参数
                 * $file_name excal文件的保存路径
                 */
                $objPHPExcel = $objReader->load($file_name, $encode = 'utf-8');//加载文件
                $sheet = $objPHPExcel->getSheet(0);//取得sheet(0)表
                $highestRow = $sheet->getHighestRow(); // 取得总行数
                $highestColumn = $sheet->getHighestColumn(); // 取得总列数
                $res = 0;
                for ($i = 1; $i <= $highestRow; $i++) {
                    $card_num = $objPHPExcel->getActiveSheet()->getCell("A" . $i)->getValue();
                    $cardData = pdo_get('cqkundian_farm_card', array('card_num' => $card_num, 'uniacid' => $uniacid));
                    if (empty($cardData)) {
                        if ($card_num != '') {
                            $data['card_num'] = $card_num;
                            $data['card_pwd'] = $objPHPExcel->getActiveSheet()->getCell("B" . $i)->getValue();
                            $data['uniacid'] = $uniacid;
                            $data['import_time'] = time();
                            $data['status'] = 0;
                            $res += pdo_insert('cqkundian_farm_card', $data);
                        }
                    }
                }
                if ($res) {
                    message('导入文件成功', url("site/entry/card", array('m' => 'kundian_farm', 'op' => 'card_index')));
                } else {
                    message('导入文件失败');
                }
            } else {
                message('文件上传失败');
            }
        } else {
            message('请选择上传文件');
            die;
        }
    }else{
        message('请选择上传文件！');
        die;
    }
}

//兑换卡删除
if($op=='card_delete'){
    $res=pdo_delete('cqkundian_farm_card',array('uniacid'=>$uniacid,'id'=>$_GPC['id']));
    echo $res ? json_encode(array('status'=>1)) :json_encode(array('status'=>2));die;
}

//兑换卡设置
if($op=='card_set'){
    $con=array(
        'uniacid'=>$uniacid,
        'ikey'=>array('card_expire_time','card_score','card_use_explain','is_open_sign_card_score','sign_title','card_tag_title'),
    );
    $setData=pdo_getall('cqkundian_farm_set',$con);
    $list=array();
    foreach ($setData as $key=>$value){
        $list[$value['ikey']]=$value['value'];
    }
    include $this->template("web/card/card_set");
}

//保存对换卡设置
if($op=='card_set_save'){
    $data=$_POST;
    $res=0;
    foreach ($data as $key => $value){
        $update_data=array(
            'ikey'=>$key,
            'value'=>$value,
            'uniacid'=>$uniacid,
        );
        $con=array(
            'ikey'=>$key,
            'uniacid'=>$uniacid,
        );
        $setData=pdo_get('cqkundian_farm_set',$con);
        if(empty($setData)){
            $res+=pdo_insert('cqkundian_farm_set',$update_data);
        }else{
            $res+=pdo_update('cqkundian_farm_set',$update_data,$con);
        }
    }
    if($res){
        message('操作成功',url('site/entry/card',array('m'=>'kundian_farm','op'=>'card_set')));
    }else{
        message("操作失败");
    }
}

//下载模板
if($op=='download_card_template'){
    $filename=$_W['siteroot'].'addons/kundian_farm/resource/card_template.xls';
    echo $filename;die;
    if(file_exists($filename)){
        header('location:'.$filename);
    }else{
        header('HTTP/1.1 404 Not Found');
    }
}